﻿ /************************************************************************
  * author :zcd
  * date :2018-04-19
  * desc:订单的增删改查
  * version：v1.0 
  *************************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL;
using Models;
using Common;
using System.Threading;

namespace 包覆门.OrderManager
{
    public partial class OrderList : Form
    {
        #region 构造方法
        public OrderList()
        {
            InitializeComponent();
            this.dgorder.AutoGenerateColumns = false;
            this.dgOrderDetail.AutoGenerateColumns = false;
            
        }
        #endregion 构造方法

        #region 自定义变量
        private DataTable dtMxbh = new DataTable();//门型编号
        private DictionaryDAL dry = new DictionaryDAL();//数据处理
        private OrderDAL dal = new OrderDAL();
        private Dictionary<int, string> dicOld = new Dictionary<int, string>();
        private Dictionary<int, string> dicGlass = new Dictionary<int, string>();
        private Dictionary<int, string> dicZh = new Dictionary<int, string>();
        private Dictionary<int, string> dicOver = new Dictionary<int, string>();
        private Dictionary<int, string> dicZt = new Dictionary<int, string>();//柱头
        private Dictionary<int, string> dicSpecial = new Dictionary<int, string>();//是否特殊门型
        private Dictionary<int, string> dicManufacture = new Dictionary<int, string>();//下生产
        private Dictionary<int, string> dicCkM = new Dictionary<int, string>();

        private int lineNum = 0;
        //private Dictionary<int, string> dicPrint = new Dictionary<int, string>();
        
        #endregion 自定义变量


        #region 按钮操作

        private void SetToolState() {
            if ("0".Equals(RemeberName.IsAdmin))
            {
                this.tsbXsc.Enabled = false;
                this.tsbCancelManufacture.Enabled = false;
            }
            else {
                this.tsbXsc.Enabled = true;
                this.tsbCancelManufacture.Enabled = true;
            }
        }

        //加载数据信息
        private void OrderList_Load(object sender, EventArgs e)
        {
            SetToolState();
            dtMxbh = dry.GetByMXBH("0");
            // DataGridViewComboBoxColumn dgvcb = dgOrderDetail.Columns["mxbh"] as DataGridViewComboBoxColumn;
            //mxbh.DataSource = dtMxbh;
            //mxbh.DisplayMember = "mxsm";
           // mxbh.ValueMember = "mxbh";
            //mxbh.DataPropertyName = "mxbh";

            dicOld.Add(0,"否");
            dicOld.Add(1,"是");

            dicZt.Add(0, "否");
            dicZt.Add(1, "是");

            dicSpecial.Add(0, "否");
            dicSpecial.Add(1, "是");

            dicGlass.Add(0,"否");
            dicGlass.Add(1, "是");

            dicZh.Add(0,"否");
            dicZh.Add(1,"是(默认)");
            dicZh.Add(2, "是(自定义)");

            dicOver.Add(0,"未确认");
            dicOver.Add(1,"已确认");

            dicManufacture.Add(0,"未投产");
            dicManufacture.Add(1,"已生产");

            dicCkM.Add(-1,"所有");
            dicCkM.Add(0, "未投产");
            dicCkM.Add(1, "已生产");

            BindingSource bsZt = new BindingSource();
            bsZt.DataSource = dicZt;
            this.iszt.DataSource = bsZt;
            this.iszt.DisplayMember = "value";
            this.iszt.ValueMember = "key";

            BindingSource bsSpecial = new BindingSource();
            bsSpecial.DataSource = dicSpecial;
            this.isspecial.DataSource = bsSpecial;
            this.isspecial.DisplayMember = "value";
            this.isspecial.ValueMember = "key";

            BindingSource bsOld = new BindingSource();
            bsOld.DataSource = dicOld;
            this.isold.DataSource = bsOld;
            this.isold.DisplayMember = "value";
            this.isold.ValueMember = "key";

            BindingSource bsGlass = new BindingSource();
            bsGlass.DataSource = dicGlass;
            this.isglass.DataSource = bsGlass;
            this.isglass.DisplayMember = "value";
            this.isglass.ValueMember = "key";

            BindingSource bsZh =new BindingSource();
            bsZh.DataSource = dicZh;
            this.iszh.DataSource = bsZh;
            this.iszh.DisplayMember = "value";
            this.iszh.ValueMember = "key";

            BindingSource bsOver = new BindingSource();
            bsOver.DataSource = dicOver;
            this.isover.DataSource = bsOver;
            this.isover.DisplayMember = "value";
            this.isover.ValueMember = "key";

            BindingSource bsManufacture = new BindingSource();
            bsManufacture.DataSource = dicManufacture;
            this.isManufacture.DataSource = bsManufacture;
            this.isManufacture.DisplayMember = "value";
            this.isManufacture.ValueMember = "key";

            BindingSource bsCkm = new BindingSource();
            bsManufacture.DataSource = dicCkM;
            this.cbManufacture.DataSource = bsManufacture;
            this.cbManufacture.DisplayMember = "value";
            this.cbManufacture.ValueMember = "key";
            //分页
            this.pageControl1.PageCurrent = 1;
            pageControl1.PageSize = 10;
            this.pageControl1.Bind();  

            LoadData();
            
        }
        //关闭前操作
        private void TestForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            mainform.ol = null;
        }
        //主表新增一行
        private void tsbAdd_Click(object sender, EventArgs e)
        {
            //dgorder.Rows.Add();
            MainOrderEditing moe = new MainOrderEditing();
            moe.IsNew = true;
            if (moe.ShowDialog() == DialogResult.OK) {
                LoadData();
            }
        }
        
        
        //详情表新增一行
        private void btnAddLine_Click(object sender, EventArgs e)
        {
     
            //1、首先判断是否选中主订单号
            //2、判断是否已经确认订单
            //3、判断当前用户是否与主单号用户一致
            if (!ValidateMainField()) {
                return;
            }
            //4、新增包覆门信息
            DetailOrderEditing de = new DetailOrderEditing();
            de.IsNew = true;
            de.IsCopy = false;
            de.Danhao = this.dgorder.SelectedRows[0].Cells["danhao"].Value.ToString();
            lineNum=this.dgorder.SelectedRows[0].Index ;
            if (de.ShowDialog() == DialogResult.OK) {
                LoadData();
                dgorder.Rows[lineNum].Cells["danhao"].Selected = true; //dgvFeeInfo为datagridview的名称
                dgorder.CurrentCell = dgorder.Rows[lineNum].Cells["danhao"];
            }
            
        }
        
        //搜索
        private void tsbSearch_Click(object sender, EventArgs e)
        {
            //dgOrderDetail.DataSource = dal.GetOrderDetail();
            LoadData();
           
        }
        //删除主订单
        private void tsbDel_Click(object sender, EventArgs e)
        {

            //1、判断是否已经选中行
           
            //2、判断是否已经确定订单

            //3、判断是否是添加用户编辑
            if (!ValidateMainField())//合并为公共方法
            {
                return;
            }

            //3、是否存在包覆门子订单
            DataTable detail = new DataTable();

            
            string danhao = this.dgorder.SelectedRows[0].Cells["danhao"].Value.ToString();
            detail = dal.GetDetailOrderByDanhao(danhao);
            int ss = 1 + detail.Rows.Count;
            if (detail.Rows.Count > 0)//当有子订单时提示
            {
                if (MessageBox.Show("该订单下有" + detail.Rows.Count + "条数据,确认删除么？", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                {
                    //删除
                    int result = dal.DelMainOrderByDanhao(danhao);
                    if (result == ss)
                    {
                        MessageBox.Show("删除成功");
                        LoadData();
                    }
                }
                
            }
            else { //当无子订单时提示

                if (MessageBox.Show("确定删除单号："+danhao+"？", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                {
                    //删除
                    int result = dal.DelMainOrderByDanhao(danhao);
                    if (result == ss)
                    {
                        MessageBox.Show("删除成功");
                        LoadData();
                    }
                }
            }
        }
        //编辑订单编辑
        private void tsbEdit_Click(object sender, EventArgs e)
        {
            //1、判断是否已经选中行
            
            //2、判断是否已经确定订单
            
            //3、判断是否是添加用户编辑
            if (!ValidateMainField())
            {
                return;
            }

            //将主键id或isnew传入子窗口
            MainOrderEditing moe = new MainOrderEditing();
            moe.EditingID = this.dgorder.SelectedRows[0].Cells["mid"].Value.ToString();
            moe.OrderNum = this.dgorder.SelectedRows[0].Cells["danhao"].Value.ToString();
            moe.IsNew = false;
            lineNum = this.dgorder.SelectedRows[0].Index;
            if (moe.ShowDialog() == DialogResult.OK)
            {

                LoadData();
            }
            dgorder.Rows[lineNum].Cells["danhao"].Selected = true; //dgvFeeInfo为datagridview的名称
            dgorder.CurrentCell = dgorder.Rows[lineNum].Cells["danhao"];
        }
        //打印订单
        private void tsbOrderPrint_Click(object sender, EventArgs e)
        {
            //1、是否选中
            int count = this.dgorder.SelectedRows.Count;
            if (count <= 0)
            {
                MessageBox.Show("请选中要打印的行");
                return;
            }
            //2、判断是否已经确定订单
            int isover = int.Parse(this.dgorder.SelectedRows[0].Cells["isover"].Value.ToString());
            if (isover != 1)
            {
                MessageBox.Show("该订单未确认");
                return ;
            }
            lineNum = this.dgorder.SelectedRows[0].Index;
            //3、暂时不用考虑是否是录入用户才可打印
            //4、打印
            DotonView dv = new DotonView();
            dv.Danhao = this.dgorder.SelectedRows[0].Cells["danhao"].Value.ToString();
            dv.ShowDialog();
            dgorder.Rows[lineNum].Cells["danhao"].Selected = true; //dgvFeeInfo为datagridview的名称
            dgorder.CurrentCell = dgorder.Rows[lineNum].Cells["danhao"];
           
        }
        //打印料单
        private void tsbWorkPrint_Click(object sender, EventArgs e)
        {
            //1、是否选中
            int count = this.dgorder.SelectedRows.Count;
            if (count <= 0)
            {
                MessageBox.Show("请选中要打印的行");
                return;
            }
            //2、判断是否已经确定订单
            int isover = int.Parse(this.dgorder.SelectedRows[0].Cells["isover"].Value.ToString());
            if (isover != 1)
            {
                MessageBox.Show("该订单未确认");
                return;
            }
            lineNum = this.dgorder.SelectedRows[0].Index;
            //3、暂时不用考虑是否是录入用户才可打印
            //4、打印
            DotonWlView dwl = new DotonWlView();
            dwl.Danhao = this.dgorder.SelectedRows[0].Cells["danhao"].Value.ToString();
            dwl.ShowDialog();
            //lineNum = this.dgorder.SelectedRows[0].Index;
            dgorder.Rows[lineNum].Cells["danhao"].Selected = true; //dgvFeeInfo为datagridview的名称
            dgorder.CurrentCell = dgorder.Rows[lineNum].Cells["danhao"];
        }
        //确认订单
        private void tsbEnsure_Click(object sender, EventArgs e)
        {
            //1、是否选中
            int count = this.dgorder.SelectedRows.Count;
            if(count<=0){
                MessageBox.Show("请选中要确定订单的行");
                return;
            }
            //2、是否是当前用户
            string username = this.dgorder.SelectedRows[0].Cells["username"].Value.ToString();
            // object isadmin = dal.GetIsAdminByName(username);
            //int admin = isadmin == null ? 0 : Convert.ToInt32(isadmin);
            if ("0".Equals(RemeberName.IsAdmin))
            {
                if (!username.Equals(RemeberName.Username))
                {
                    MessageBox.Show("您非录入此订单用户，无法更改,请联系管理员");
                    return ;
                }
            }
            //if (!this.dgorder.SelectedRows[0].Cells["username"].Value.ToString().Equals(RemeberName.Username)) {
            //    MessageBox.Show("您非录入此订单用户，无法更改,请联系管理员");
            //    return;
            //}
            //3、是否有子订单
            DataTable dt= new DataTable();
            dt=dal.GetDetailOrderByDanhao(this.dgorder.SelectedRows[0].Cells["danhao"].Value.ToString());

            if (dt.Rows.Count<=0)
            {
                MessageBox.Show("该订单下没有包覆门和装饰线条信息，不能确定");
                return;
            }
            //*、是否已经确认生产，切勿重复确认订单
            object o1 = dal.QueryMainOrderByDanhaoIsManufacture(this.dgorder.SelectedRows[0].Cells["danhao"].Value.ToString());
            if (o1.ToString().Equals("1"))
            {
                MessageBox.Show("该订单已经确认生产，请勿重复确认");
                return;
            }

            //4、是否已经确认，切勿重复确认订单
            object o = dal.QueryMainOrderByDanhaoIsEnsure(this.dgorder.SelectedRows[0].Cells["danhao"].Value.ToString());
            if (o.ToString().Equals("1")) {
                MessageBox.Show("该订单已经确认，请勿重复确认");
                return;
            }


            if (DialogResult.OK != MessageBox.Show("确认订单", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)) return;
            //5、查询订单明细根据数量进行拆分标签
            DataTable detail = dal.QueryDetailOrderByDanhao(this.dgorder.SelectedRows[0].Cells["danhao"].Value.ToString());
            DataTable detailInfo = detail.Clone();
            detailInfo.TableName = "detailorderinfo";
            DataRow di = null;
            foreach(DataRow d in detail.Rows){
                for (int i = 0; i < Convert.ToInt32(d["num"]);i++ )
                {
                    di = detailInfo.NewRow();
                    di["did"] = d["did"];
                    di["danhao"] = d["danhao"];
                    di["zslb"] = d["zslb"];
                    di["mxbh"] = d["mxbh"];
                    di["mxbhsm"] = d["mxbhsm"];
                    di["gao"] = d["gao"];
                    di["kuan"] = d["kuan"];
                    di["xbxs"] = d["xbxs"];
                    di["num"] = 1;
                    di["direction"] = d["direction"];
                    di["color"] = d["color"];
                    di["addTime"] = DateTime.Now.ToString();
                    di["editUser"] = RemeberName.Username;
                    di["iszh"] = d["iszh"];
                    di["isdel"] = d["isdel"];
                    di["isover"] = 1;
                    if (d["zslb"].ToString().Equals("1"))
                    {
                        di["barcode"] = Convert.ToInt32(d["gao"]).ToString("0000")+"0000003";
                    }
                    else {
                        //i.ToString("00");
                        int gao =Convert.ToInt32(d["gao"]);
                        int kuan =Convert.ToInt32(d["kuan"]);
                        if(gao<300||kuan<300){
                            gao=gao+150;
                            kuan=kuan+150;
                        }
                        if (gao > kuan)
                        {
                            di["barcode"] = gao.ToString("0000") + kuan.ToString("0000") + "003";
                        }
                        else {
                            di["barcode"] = kuan.ToString("0000") + gao.ToString("0000") + "003";
                        }
                    }
                    di["iszt"] = d["iszt"];
                    detailInfo.Rows.Add(di);
                }
            }
            //6、符合要求进行确认订单  分为主订单和详细订单 通过单号来确认
            bool result = dal.EnsureOrder(this.dgorder.SelectedRows[0].Cells["danhao"].Value.ToString());
            //记录行号
            lineNum = this.dgorder.SelectedRows[0].Index;
            if (result)
            {
                string s = "确认成功,";
                //7、保存拆分明细
                int r = dal.SaveBatchInfo(detailInfo);
                if (r > 0)
                {
                    s += "标签拆分成功";
                    MessageBox.Show(s);
                }
                else {
                    s += "标签拆分失败";
                    MessageBox.Show(s);
                }
            }
            else {
                MessageBox.Show("确认失败");
            }
            LoadData();
            dgorder.Rows[lineNum].Cells["danhao"].Selected = true; //dgvFeeInfo为datagridview的名称
            dgorder.CurrentCell = dgorder.Rows[lineNum].Cells["danhao"];
        }
        //取消订单
        private void tsbCancel_Click(object sender, EventArgs e)
        {
            
            //1、是否选中
            int count = this.dgorder.SelectedRows.Count;
            if (count <= 0)
            {
                MessageBox.Show("请选中要取消订单的行");
                return;
            }
            //2、是否是当前用户
            string username = this.dgorder.SelectedRows[0].Cells["username"].Value.ToString();
            // object isadmin = dal.GetIsAdminByName(username);
            //int admin = isadmin == null ? 0 : Convert.ToInt32(isadmin);
            if ("0".Equals(RemeberName.IsAdmin))
            {
                if (!username.Equals(RemeberName.Username))
                {
                    MessageBox.Show("您非录入此订单用户，无法更改,请联系管理员");
                    return ;
                }
            }
            //*、是否已经确认生产，切勿重复确认订单
            object o1 = dal.QueryMainOrderByDanhaoIsManufacture(this.dgorder.SelectedRows[0].Cells["danhao"].Value.ToString());
            if (o1.ToString().Equals("1"))
            {
                MessageBox.Show("该订单已经确认生产，如需取消,请联系车间主任");
                return;
            }
            //if (!this.dgorder.SelectedRows[0].Cells["username"].Value.ToString().Equals(RemeberName.Username))
            //{
            //    MessageBox.Show("您非录入此订单用户，无法更改,请联系管理员");
            //    return;
            //}
            //3、是否有子订单
            //DataTable dt = new DataTable();
            //dt = dal.GetDetailOrderByDanhao(this.dgorder.SelectedRows[0].Cells["danhao"].Value.ToString());

            //if (dt.Rows.Count <= 0)
            //{
            //    MessageBox.Show("该订单下没有包覆门和装饰线条信息，不能确定");
            //    return;
            //}
            if (DialogResult.OK != MessageBox.Show("取消确认", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)) return;
            //4、符合要求进行确认订单  分为主订单和详细订单 通过单号来确认
            bool result = dal.CancelOrder(this.dgorder.SelectedRows[0].Cells["danhao"].Value.ToString());
            lineNum = this.dgorder.SelectedRows[0].Index;
            if (result)
            {
                MessageBox.Show("取消成功");
               
            }
            else
            {
                MessageBox.Show("取消失败");
               
            }
            LoadData();
            dgorder.Rows[lineNum].Cells["danhao"].Selected = true; //dgvFeeInfo为datagridview的名称
            dgorder.CurrentCell = dgorder.Rows[lineNum].Cells["danhao"];
           // lineNum = this.dgorder.SelectedRows[0].Index;
        }
       
        //详情删除选中行
        private void btnDelLine_Click(object sender, EventArgs e)
        {
            //1、判断是否已经选中行

            //2、判断是否已经确定订单

            //3、判断是否是添加用户编辑
            if (!ValidateMainField())
            {
                return;
            }
            if (this.dgOrderDetail.Rows.Count <= 0)
            {
                MessageBox.Show("没有要编辑的数据，请先增加明细数据");
                return;
            }
            string did = this.dgOrderDetail.SelectedRows[0].Cells["did"].Value.ToString();
            lineNum = this.dgorder.SelectedRows[0].Index;
            if (MessageBox.Show("确认删除此条数据么", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK) {
                int result = dal.DelDetailOrderById(did);
                if (result == 1)
                {
                    MessageBox.Show("删除成功");
                    LoadData();
                    dgorder.Rows[lineNum].Cells["danhao"].Selected = true; //dgvFeeInfo为datagridview的名称
                    dgorder.CurrentCell = dgorder.Rows[lineNum].Cells["danhao"];
                }
                else {
                    MessageBox.Show("删除失败");
                }
            }
               


        }
        //详情编辑
        private void btnEdit_Click(object sender, EventArgs e)
        {
            //1、判断是否已经选中行

            //2、判断是否已经确定订单

            //3、判断是否是添加用户编辑
            if (!ValidateMainField())
            {
                return;
            }
            if (this.dgOrderDetail.Rows.Count <= 0) {
                MessageBox.Show("没有要编辑的数据，请先增加明细数据");
                return;
            }
            //出来吧 我的详情页
            DetailOrderEditing dlo = new DetailOrderEditing();
            dlo.EditingId = this.dgOrderDetail.SelectedRows[0].Cells["did"].Value.ToString();
            dlo.IsNew = false;
            dlo.IsCopy = false;
            lineNum = this.dgorder.SelectedRows[0].Index;
            if (dlo.ShowDialog() == DialogResult.OK)
            {
                LoadData();
                dgorder.Rows[lineNum].Cells["danhao"].Selected = true; //dgvFeeInfo为datagridview的名称
                dgorder.CurrentCell = dgorder.Rows[lineNum].Cells["danhao"];
            }
        }

        //查询
        private void btnQuery_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        //关闭
        private void tsbClose_Click(object sender, EventArgs e)
        {
            Close();
            this.Dispose();
        }
        //行变化
        private void dgorder_SelectionChanged(object sender, EventArgs e)
        {
            int count = this.dgorder.SelectedRows.Count;
            if (count <= 0)
            {
                this.dgOrderDetail.DataSource = null;
                return;
            }
            else {
                string danhao = this.dgorder.SelectedRows[0].Cells["danhao"].Value.ToString();
                DataTable dt =dal.GetDetailOrderByDanhao(danhao);
                this.dgOrderDetail.DataSource = dt;
            } 
        }

        #endregion 按钮操作

        #region 自定义方法
        //按钮开关
        private void EnableButton()
        {

        }
        //按钮开关
        private void DisableButton()
        {

        }
       

        //验证 是否选中 、是否确认、是否是当前用户
        private bool ValidateMainField() {
            bool flag = true;
            //1、判断是否已经选中行
            int count = this.dgorder.SelectedRows.Count;
            if (count <= 0)
            {
                MessageBox.Show("请选择要编辑的行");
                return false;
            } 
            //2、判断是否已经确定订单
            int isover = int.Parse(this.dgorder.SelectedRows[0].Cells["isover"].Value.ToString());
            if (isover == 1)
            {
                MessageBox.Show("该订单已经确认订单");
                return false;
            }
            //3、判断是否是添加用户编辑\
            string username = this.dgorder.SelectedRows[0].Cells["adduser"].Value.ToString();
           // object isadmin = dal.GetIsAdminByName(username);
            //int admin = isadmin == null ? 0 : Convert.ToInt32(isadmin);
            if ("0".Equals(RemeberName.IsAdmin))
            {
                if (!username.Equals(RemeberName.Username))
                {
                    MessageBox.Show("您非录入此订单用户，无法更改,请联系管理员");
                    return false;
                }
            }
            return flag;
        }
        //加载数据
        private int LoadData()
        {
            string ordernum = this.tbxOrder.Text.Trim();
            string contract = this.tbContract.Text.Trim();
            string sname = this.tbName.Text.Trim();
            string phone = this.tbPhone.Text.Trim();
            string isManufacture = this.cbManufacture.SelectedValue == null ? "" : this.cbManufacture.SelectedValue.ToString();

            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("ordernum", ordernum);
            dic.Add("contract", contract);
            dic.Add("sname", sname);
            dic.Add("phone", phone);

            dic.Add("isManufacture", isManufacture);

            DataTable dt = dal.ListMainOrderAll(dic,pageControl1.PageCurrent,pageControl1.PageSize);
            this.pageControl1.Sourceobj = dt;
            this.dgorder.DataSource = dt;
            return dal.ListMainOrderAll(dic).Rows.Count;

        }

        
     

       
        #endregion 自定义方法
        /// <summary>  
        /// 分页控件事件  
        /// </summary>  
        /// <param name="e"></param>  
        /// <returns></returns>  
        private int pageControl1_EventPaging_1(EventPagingArg e)
        {
            return LoadData();
        }
        //条形码打印
        private void tbBarcode_Click(object sender, EventArgs e)
        {
            MessageBox.Show("正在开发中");
            return;
            //1、是否选中
            int count = this.dgorder.SelectedRows.Count;
            if (count <= 0)
            {
                MessageBox.Show("请选中要打印的行");
                return;
            }
            //2、判断是否已经确定订单
            int isover = int.Parse(this.dgorder.SelectedRows[0].Cells["isover"].Value.ToString());
            if (isover != 1)
            {
                MessageBox.Show("该订单未确认");
                return;
            }
            lineNum = this.dgorder.SelectedRows[0].Index;
            //3、暂时不用考虑是否是录入用户才可打印
            //4、打印 V2.0
            //DotonBarcodeView dbv = new DotonBarcodeView();
            //dbv.Danhao = this.dgorder.SelectedRows[0].Cells["danhao"].Value.ToString();
            //dbv.ShowDialog();
            //lineNum = this.dgorder.SelectedRows[0].Index;
            dgorder.Rows[lineNum].Cells["danhao"].Selected = true; //dgvFeeInfo为datagridview的名称
            dgorder.CurrentCell = dgorder.Rows[lineNum].Cells["danhao"];
            
        }
        //大唐订单导出
        private void tsbDTOrder_Click(object sender, EventArgs e)
        {
            ExportOrderExcel ee = new ExportOrderExcel();
            ee.Danhao = "987654321";
            ee.OutExcel();
        }
        //OEm订单打印
        private void tsbOEMPrint_Click(object sender, EventArgs e)
        {
            //1、是否选中
            int count = this.dgorder.SelectedRows.Count;
            if (count <= 0)
            {
                MessageBox.Show("请选中要打印的行");
                return;
            }
            //2、判断是否已经确定订单
            int isover = int.Parse(this.dgorder.SelectedRows[0].Cells["isover"].Value.ToString());
            if (isover != 1)
            {
                MessageBox.Show("该订单未确认");
                return;
            }
            lineNum = this.dgorder.SelectedRows[0].Index;
            //3、暂时不用考虑是否是录入用户才可打印
            //4、打印
            DotonView dv = new DotonView();
            dv.IsDoton = false;
            dv.Danhao = this.dgorder.SelectedRows[0].Cells["danhao"].Value.ToString();
            dv.ShowDialog();
            dgorder.Rows[lineNum].Cells["danhao"].Selected = true; //dgvFeeInfo为datagridview的名称
            dgorder.CurrentCell = dgorder.Rows[lineNum].Cells["danhao"];
        }
        //大唐订单打印
        private void tsbDotonPrint_Click(object sender, EventArgs e)
        {
            //1、是否选中
            int count = this.dgorder.SelectedRows.Count;
            if (count <= 0)
            {
                MessageBox.Show("请选中要打印的行");
                return;
            }
            //2、判断是否已经确定订单
            int isover = int.Parse(this.dgorder.SelectedRows[0].Cells["isover"].Value.ToString());
            if (isover != 1)
            {
                MessageBox.Show("该订单未确认");
                return;
            }
            lineNum = this.dgorder.SelectedRows[0].Index;
            //3、暂时不用考虑是否是录入用户才可打印
            //4、打印
            DotonView dv = new DotonView();
            dv.IsDoton = true;
            dv.Danhao = this.dgorder.SelectedRows[0].Cells["danhao"].Value.ToString();
            dv.ShowDialog();
            dgorder.Rows[lineNum].Cells["danhao"].Selected = true; //dgvFeeInfo为datagridview的名称
            dgorder.CurrentCell = dgorder.Rows[lineNum].Cells["danhao"];
        }
        //大唐订单导出
        private void tsbDotonExcel_Click(object sender, EventArgs e)
        {
            
            //1、是否选中
            int count = this.dgorder.SelectedRows.Count;
            if (count <= 0)
            {
                MessageBox.Show("请选中要打印的行");
                return;
            }
            //2、判断是否已经确定订单
            int isover = int.Parse(this.dgorder.SelectedRows[0].Cells["isover"].Value.ToString());
            if (isover != 1)
            {
                MessageBox.Show("该订单未确认");
                return;
            }
            lineNum = this.dgorder.SelectedRows[0].Index;
            //3、暂时不用考虑是否是录入用户才可打印
            //4、打印
            frmWaitingBox f = new frmWaitingBox((obj, args) =>
            {
                ExportOrderExcel ee = new ExportOrderExcel();
                ee.Danhao = this.dgorder.SelectedRows[0].Cells["danhao"].Value.ToString();
                ee.IsDoton = true;
                ee.OutExcel();
            }, 60, "正在填充数据，请稍后", false, true);
            f.ShowDialog(this);

            //ExportOrderExcel ee = new ExportOrderExcel();
            //ee.Danhao = this.dgorder.SelectedRows[0].Cells["danhao"].Value.ToString();
          // ee.IsDoton = true;
            //ee.OutExcel();
            
            //LoadingControl pLoading = LoadingControl.getLoading();
            //pLoading.SetExecuteMethod(methodDoton);
            //pLoading.ShowDialog();
            dgorder.Rows[lineNum].Cells["danhao"].Selected = true; //dgvFeeInfo为datagridview的名称
            dgorder.CurrentCell = dgorder.Rows[lineNum].Cells["danhao"];
        }
        private void methodDoton()
        {
            LoadingControl pLoading = LoadingControl.getLoading();
            Thread.Sleep(1000);
            ExportOrderExcel ee = new ExportOrderExcel();
            ee.Danhao = this.dgorder.SelectedRows[0].Cells["danhao"].Value.ToString();
            ee.IsDoton = true;
            ee.OutExcel();
            LoadingControl.getLoading().CloseLoadingForm();
        }
        private void methodOem()
        {
            LoadingControl pLoading = LoadingControl.getLoading();
            ExportOrderExcel ee = new ExportOrderExcel();
            ee.Danhao = this.dgorder.SelectedRows[0].Cells["danhao"].Value.ToString();
            ee.IsDoton = false;
            ee.OutExcel();
            LoadingControl.getLoading().CloseLoadingForm();
        } 


        //oem订单导出
        private void tsbOEMExcel_Click(object sender, EventArgs e)
        {
            //1、是否选中
            int count = this.dgorder.SelectedRows.Count;
            if (count <= 0)
            {
                MessageBox.Show("请选中要打印的行");
                return;
            }
            //2、判断是否已经确定订单
            int isover = int.Parse(this.dgorder.SelectedRows[0].Cells["isover"].Value.ToString());
            if (isover != 1)
            {
                MessageBox.Show("该订单未确认");
                return;
            }
            lineNum = this.dgorder.SelectedRows[0].Index;
            //3、暂时不用考虑是否是录入用户才可打印
            //4、打印
            //LoadingControl pLoading = LoadingControl.getLoading();
            //pLoading.SetExecuteMethod(methodOem);
            //pLoading.ShowDialog();
            frmWaitingBox f = new frmWaitingBox((obj, args) =>
            {
                ExportOrderExcel ee = new ExportOrderExcel();
                ee.Danhao = this.dgorder.SelectedRows[0].Cells["danhao"].Value.ToString();
                ee.IsDoton = false;
                ee.OutExcel();
            }, 60, "正在填充数据，请稍后", false, true);
            f.ShowDialog(this);
            
            dgorder.Rows[lineNum].Cells["danhao"].Selected = true; //dgvFeeInfo为datagridview的名称
            dgorder.CurrentCell = dgorder.Rows[lineNum].Cells["danhao"];
        }
        //大唐料单打印
        private void tsbDotonWorkPrint_Click(object sender, EventArgs e)
        {
            //1、是否选中
            int count = this.dgorder.SelectedRows.Count;
            if (count <= 0)
            {
                MessageBox.Show("请选中要打印的行");
                return;
            }
            //2、判断是否已经确定订单
            int isover = int.Parse(this.dgorder.SelectedRows[0].Cells["isover"].Value.ToString());
            if (isover != 1)
            {
                MessageBox.Show("该订单未确认");
                return;
            }
            lineNum = this.dgorder.SelectedRows[0].Index;
            //3、暂时不用考虑是否是录入用户才可打印
            //4、打印
            DotonWlView dwl = new DotonWlView();
            dwl.Danhao = this.dgorder.SelectedRows[0].Cells["danhao"].Value.ToString();
            dwl.ShowDialog();
            //lineNum = this.dgorder.SelectedRows[0].Index;
            dgorder.Rows[lineNum].Cells["danhao"].Selected = true; //dgvFeeInfo为datagridview的名称
            dgorder.CurrentCell = dgorder.Rows[lineNum].Cells["danhao"];
        }
        //OEM料单打印
        private void tsbOEMworkPrint_Click(object sender, EventArgs e)
        {
            //1、是否选中
            int count = this.dgorder.SelectedRows.Count;
            if (count <= 0)
            {
                MessageBox.Show("请选中要打印的行");
                return;
            }
            //2、判断是否已经确定订单
            int isover = int.Parse(this.dgorder.SelectedRows[0].Cells["isover"].Value.ToString());
            if (isover != 1)
            {
                MessageBox.Show("该订单未确认");
                return;
            }
            lineNum = this.dgorder.SelectedRows[0].Index;
            //3、暂时不用考虑是否是录入用户才可打印
            //4、打印
            DotonWlView dwl = new DotonWlView();
            dwl.Danhao = this.dgorder.SelectedRows[0].Cells["danhao"].Value.ToString();
            dwl.ShowDialog();
            //lineNum = this.dgorder.SelectedRows[0].Index;
            dgorder.Rows[lineNum].Cells["danhao"].Selected = true; //dgvFeeInfo为datagridview的名称
            dgorder.CurrentCell = dgorder.Rows[lineNum].Cells["danhao"];
        }
        //大唐料单导出
        private void tsbDotonWorkExcel_Click(object sender, EventArgs e)
        {
            //1、是否选中
            int count = this.dgorder.SelectedRows.Count;
            if (count <= 0)
            {
                MessageBox.Show("请选中要打印的行");
                return;
            }
            //2、判断是否已经确定订单
            int isover = int.Parse(this.dgorder.SelectedRows[0].Cells["isover"].Value.ToString());
            if (isover != 1)
            {
                MessageBox.Show("该订单未确认");
                return;
            }
            lineNum = this.dgorder.SelectedRows[0].Index;
            //3、暂时不用考虑是否是录入用户才可打印
            //4、打印
            //LoadingControl pLoading = LoadingControl.getLoading();
            //pLoading.SetExecuteMethod(methodDotonWl);
            //pLoading.ShowDialog();
            frmWaitingBox f = new frmWaitingBox((obj, args) =>
            {
                ExportWlExcel ew = new ExportWlExcel();
                ew.Danhao = this.dgorder.SelectedRows[0].Cells["danhao"].Value.ToString();
                ew.IsDoton = true;
                ew.OutExcel();
            }, 60, "正在填充数据，请稍后", false, true);
            f.ShowDialog(this);
            lineNum = this.dgorder.SelectedRows[0].Index;
            dgorder.Rows[lineNum].Cells["danhao"].Selected = true; //dgvFeeInfo为datagridview的名称
            dgorder.CurrentCell = dgorder.Rows[lineNum].Cells["danhao"];
        }
        //OEM料单导出
        private void tsbOemWorkExcel_Click(object sender, EventArgs e)
        {
            //1、是否选中
            int count = this.dgorder.SelectedRows.Count;
            if (count <= 0)
            {
                MessageBox.Show("请选中要打印的行");
                return;
            }
            //2、判断是否已经确定订单
            int isover = int.Parse(this.dgorder.SelectedRows[0].Cells["isover"].Value.ToString());
            if (isover != 1)
            {
                MessageBox.Show("该订单未确认");
                return;
            }
            lineNum = this.dgorder.SelectedRows[0].Index;
            //3、暂时不用考虑是否是录入用户才可打印
            //4、打印
           // LoadingControl pLoading = LoadingControl.getLoading();
           // pLoading.SetExecuteMethod(methodOemWl);
            //pLoading.ShowDialog();
            frmWaitingBox f = new frmWaitingBox((obj, args) =>
            {
                ExportWlExcel ew = new ExportWlExcel();
                ew.Danhao = this.dgorder.SelectedRows[0].Cells["danhao"].Value.ToString();
                ew.IsDoton = false;
                ew.OutExcel();
            }, 60, "正在填充数据，请稍后", false, true);
            f.ShowDialog(this);
            //lineNum = this.dgorder.SelectedRows[0].Index;
            dgorder.Rows[lineNum].Cells["danhao"].Selected = true; //dgvFeeInfo为datagridview的名称
            dgorder.CurrentCell = dgorder.Rows[lineNum].Cells["danhao"];
        }

        private void methodDotonWl()
        {
            LoadingControl pLoading = LoadingControl.getLoading();
            ExportWlExcel ew = new ExportWlExcel();
            ew.Danhao = this.dgorder.SelectedRows[0].Cells["danhao"].Value.ToString();
            ew.IsDoton = true;
            ew.OutExcel();
            LoadingControl.getLoading().CloseLoadingForm();
        }
        private void methodOemWl()
        {
            LoadingControl pLoading = LoadingControl.getLoading();
            ExportWlExcel ew = new ExportWlExcel();
            ew.Danhao = this.dgorder.SelectedRows[0].Cells["danhao"].Value.ToString();
            ew.IsDoton = false;
            ew.OutExcel();
            LoadingControl.getLoading().CloseLoadingForm();
        }
        //复制一行
        private void btnCopy_Click(object sender, EventArgs e)
        {
            //1、判断是否已经选中行

            //2、判断是否已经确定订单

            //3、判断是否是添加用户编辑
            if (!ValidateMainField())
            {
                return;
            }
            if (this.dgOrderDetail.Rows.Count <= 0)
            {
                MessageBox.Show("没有要编辑的数据，请先增加明细数据");
                return;
            }
            //复制
            DetailOrderEditing dlo = new DetailOrderEditing();
            dlo.EditingId = this.dgOrderDetail.SelectedRows[0].Cells["did"].Value.ToString();
            dlo.IsNew = false;
            dlo.IsCopy = true;
            lineNum = this.dgorder.SelectedRows[0].Index;
            if (dlo.ShowDialog() == DialogResult.OK)
            {
                LoadData();
                dgorder.Rows[lineNum].Cells["danhao"].Selected = true; //dgvFeeInfo为datagridview的名称
                dgorder.CurrentCell = dgorder.Rows[lineNum].Cells["danhao"];
            }
        }
        //大唐无报价打印
        private void tsbDotonNopricePrint_Click(object sender, EventArgs e)
        {
            //1、是否选中
            int count = this.dgorder.SelectedRows.Count;
            if (count <= 0)
            {
                MessageBox.Show("请选中要打印的行");
                return;
            }
            //2、判断是否已经确定订单
            int isover = int.Parse(this.dgorder.SelectedRows[0].Cells["isover"].Value.ToString());
            if (isover != 1)
            {
                MessageBox.Show("该订单未确认");
                return;
            }
            lineNum = this.dgorder.SelectedRows[0].Index;
            //3、暂时不用考虑是否是录入用户才可打印
            //4、打印
            DotonNoPriceView dv = new DotonNoPriceView();
            dv.IsDoton = true;
            dv.Danhao = this.dgorder.SelectedRows[0].Cells["danhao"].Value.ToString();
            dv.ShowDialog();
            dgorder.Rows[lineNum].Cells["danhao"].Selected = true; //dgvFeeInfo为datagridview的名称
            dgorder.CurrentCell = dgorder.Rows[lineNum].Cells["danhao"];
        }
        //OEM无报价打印
        private void OEMNoPricePrint_Click(object sender, EventArgs e)
        {
            //1、是否选中
            int count = this.dgorder.SelectedRows.Count;
            if (count <= 0)
            {
                MessageBox.Show("请选中要打印的行");
                return;
            }
            //2、判断是否已经确定订单
            int isover = int.Parse(this.dgorder.SelectedRows[0].Cells["isover"].Value.ToString());
            if (isover != 1)
            {
                MessageBox.Show("该订单未确认");
                return;
            }
            lineNum = this.dgorder.SelectedRows[0].Index;
            //3、暂时不用考虑是否是录入用户才可打印
            //4、打印
            DotonNoPriceView dv = new DotonNoPriceView();
            dv.IsDoton = false;
            dv.Danhao = this.dgorder.SelectedRows[0].Cells["danhao"].Value.ToString();
            dv.ShowDialog();
            dgorder.Rows[lineNum].Cells["danhao"].Selected = true; //dgvFeeInfo为datagridview的名称
            dgorder.CurrentCell = dgorder.Rows[lineNum].Cells["danhao"];
        }
        //添加自增序号
        private void dgOrderDetail_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
            e.RowBounds.Location.Y,
            this.dgOrderDetail.RowHeadersWidth - 4,
            e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics,
                  (e.RowIndex + 1).ToString(),
                   dgOrderDetail.RowHeadersDefaultCellStyle.Font,
                   rectangle,
                   dgOrderDetail.RowHeadersDefaultCellStyle.ForeColor,
                   TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }
        //主单号增加序号
        private void dgorder_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
            e.RowBounds.Location.Y,
            this.dgorder.RowHeadersWidth - 4,
            e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics,
                  (e.RowIndex + 1).ToString(),
                   dgorder.RowHeadersDefaultCellStyle.Font,
                   rectangle,
                   dgorder.RowHeadersDefaultCellStyle.ForeColor,
                   TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }
        //清空
        private void btnClearText_Click(object sender, EventArgs e)
        {
            this.tbxOrder.Text = "";
            this.tbContract.Text = "";
            this.tbName.Text = "";
            this.tbPhone.Text = "";
            this.cbManufacture.SelectedIndex = 0;
        }
        //下生产
        private void tsbXsc_Click(object sender, EventArgs e)
        {
            //1、是否选中
            int count = this.dgorder.SelectedRows.Count;
            if (count <= 0)
            {
                MessageBox.Show("请选中要确定下生产订单的行");
                return;
            }
            //2、是否是当前用户
            //string username = this.dgorder.SelectedRows[0].Cells["username"].Value.ToString();
            // object isadmin = dal.GetIsAdminByName(username);
            //int admin = isadmin == null ? 0 : Convert.ToInt32(isadmin);
            if ("0".Equals(RemeberName.IsAdmin))
            {
                MessageBox.Show("您没有此权限，请联系管理员");
                return;
            }

            string isover = this.dgorder.SelectedRows[0].Cells["isover"].Value.ToString();
            if (!isover.Equals("1"))
            {
                MessageBox.Show("此订单未确认,不能下生产");
                return;
            }

            //4、是否已经确认生产，切勿重复确认订单
            object o = dal.QueryMainOrderByDanhaoIsManufacture(this.dgorder.SelectedRows[0].Cells["danhao"].Value.ToString());
            if (o.ToString().Equals("1"))
            {
                MessageBox.Show("该订单已经确认生产，请勿重复确认");
                return;
            }


            if (DialogResult.OK != MessageBox.Show("确认生产", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)) return;
            
            //5、符合要求进行确认订单  分为主订单和详细订单 通过单号来确认
            bool result = dal.EnsureManufactureOrder(this.dgorder.SelectedRows[0].Cells["danhao"].Value.ToString());
            //记录行号
            lineNum = this.dgorder.SelectedRows[0].Index;
            if (result)
            {
                string s = "确认成功,";
                MessageBox.Show(s);
            }
            else
            {
                MessageBox.Show("确认失败");
            }
            LoadData();
            dgorder.Rows[lineNum].Cells["danhao"].Selected = true; //dgvFeeInfo为datagridview的名称
            dgorder.CurrentCell = dgorder.Rows[lineNum].Cells["danhao"];
        }
        //取消生产
        private void tsbCancelManufacture_Click(object sender, EventArgs e)
        {
            //1、是否选中
            int count = this.dgorder.SelectedRows.Count;
            if (count <= 0)
            {
                MessageBox.Show("请选中要取消下生产订单的行");
                return;
            }
            //2、是否是当前用户
            //string username = this.dgorder.SelectedRows[0].Cells["username"].Value.ToString();
            // object isadmin = dal.GetIsAdminByName(username);
            //int admin = isadmin == null ? 0 : Convert.ToInt32(isadmin);
            if ("0".Equals(RemeberName.IsAdmin))
            {
                MessageBox.Show("您没有此权限，请联系管理员");
                return;
            }

            //3、是否已经取消生产，切勿重复确认订单
            object o = dal.QueryMainOrderByDanhaoIsManufacture(this.dgorder.SelectedRows[0].Cells["danhao"].Value.ToString());
            if (o.ToString().Equals("0"))
            {
                MessageBox.Show("该订单并未生产，请勿重复取消");
                return;
            }

            if (DialogResult.OK != MessageBox.Show("取消生产", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)) return;

            //5、符合要求进行确认订单  分为主订单和详细订单 通过单号来确认
            bool result = dal.CancelManufactureOrder(this.dgorder.SelectedRows[0].Cells["danhao"].Value.ToString());
            //记录行号
            lineNum = this.dgorder.SelectedRows[0].Index;
            if (result)
            {
                string s = "取消成功";
                MessageBox.Show(s);
            }
            else
            {
                MessageBox.Show("取消失败");
            }
            LoadData();
            dgorder.Rows[lineNum].Cells["danhao"].Selected = true; //dgvFeeInfo为datagridview的名称
            dgorder.CurrentCell = dgorder.Rows[lineNum].Cells["danhao"];
        } 
    }
}
